﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using S4Store.Service;
using S4Store.Domain.Report;
using S4Store.Models.AfterSaleCarKPI;
using S4Store.Domain.ExhibitionKpi;

namespace S4Store.Controllers
{
    public class AfterSaleMonthConfigController : ReportBaseController
    {
        public AfterSaleCarKPIService AfterSaleCarKPIService { get; set; }

        public ActionResult Index()
        {
            AfterSaleMonthConfigModel model = new AfterSaleMonthConfigModel();
            this.InjectReportModel(model);
            model.BarModel.AddView(Models.ReportBarModel.ViewItem.Month);
            model.BarModel.Action = "Index";
            model.BarModel.Controller = "AfterSaleMonthConfig";
            model.BarModel.RemoveView(Models.ReportBarModel.ViewItem.Stores);
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                var config = AfterSaleCarKPIService.GetAfterSaleMonthStandardConfig(Month.Year, Month.Month, CurrentUser.StoreId.Value);
                if (config == null) config = new Domain.AfterSaleKpi.MonthStandardConfig();
                model.config = config;
                t.Commit();
                return View(model);
            }
        }

        public JsonResult SaveAfterSaleMonthConfig(Domain.AfterSaleKpi.MonthStandardConfig config)
        {
            using (var t = new Transtion())
            {
                t.Inject(AfterSaleCarKPIService);
                config.IsConfirmed = false;
                config.Month = Month;
                config.StoreId = CurrentUser.Store.Id;
                AfterSaleCarKPIService.SubmitSaleMonthStandardConfig(Month.Year, Month.Month, CurrentUser.Store.Id, config);
                t.Commit();
                return new JsonResult
                {
                    Data = new
                    {
                        success = true
                    }
                };
            }
        }

    }
}
